<?php
namespace ZHMVC\DB\MANAGER;

if (! isset($_SESSION)) {
    session_start();
}
include (dirname(dirname(__FILE__)) . "/zhconfig/Config.php");
include (ZH_PATH . DS . MANAGERFPATH . DS . 'islogin' . ZH);

$isp = new \ZHMVC\D\MANAGER\isPermission();
$isper = $isp->getPermission();
$_curlid = $isp->getCUrl();
$c = new \ZHCONFIG\ZhConfig();
$db_pre = $c->getDbPre();

if($isper==1)
{
    $ErrMsg="对不起，你没有访问该页面的权限";
    echo $ErrMsg;
    exit;
}
elseif($isper==0)
{
    $ErrMsg="对不起，地址错误";
    echo $ErrMsg;
    exit;
}

include (ZH_PATH . DS . MANAGERFPATH . DS . 'top1' . ZH);
?>
<div class="wap-container">
	<nav class="breadcrumb" style="background-color:#fff;padding: 0 24px">
	备份还原管理
	<a class="btn btn-success radius f-r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
	</nav>
<?php 
$action=SafeRequest(getPGC('atcion'),0);
switch ($action)
{
    case "huifu":
        huifu($db_pre);
		break;
	case "add":
		add($db_pre);
		break;
	case "del":
		del($db_pre);
		break;
	default:
		main($db_pre);
}

function main($db_pre)
{
?>
<article class="Hui-admin-content clearfix">
<div class="panel mt-20">
	<div class="panel-body">
		<div class="clearfix">
			<span class="f-l">
				<a href="javascript:;" onClick="location='?atcion=add';" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 备份</a>
			</span>
		</div>
		<div class="clearfix mt-20">
            <table class="table table-border table-bordered table-bg table-hover table-sort">
              <thead>
                <tr class="text-c">
    <th>ID</th>
    <th>名称</th>
    <th>备份时间</th>
	<th>操作</th>
  </tr>
              </thead>
              <tbody>
  <?php
        $rs = D("zhmvc_mysql")->order('id desc')->getLinkAll("id,`name`,`addtime`", true);
        $datas = $rs['datas'];
        $rows = $rs['rows'];
	for($j=0;$j<$rows;$j++)
	{
		$data = $datas[$j];
	?>
  <tr class="text-c"> 
    <td><?php echo $data['id'];?></td>
    <td><?php echo $data['name'];?></td>
    <td><?php echo $data['addtime'];?></td>
    <td class="taskOptions">
    <a href="?atcion=huifu&id=<?php echo $data['id'];?>" onclick="{if(confirm('确定恢复吗?')){return true;}return false;}">恢复</a>
    | <a href="?atcion=del&id=<?php echo $data['id'];?>" onclick="{if(confirm('确定删除吗?')){return true;}return false;}">删除</a>
    </td>
  </tr>
        <?php
        }
        ?>
              </tbody>
            </table>
					</div>
				</div>
			</div>
</article>
  
<?php 
}



function add($db_pre)
{

    $name=date('Ymdhis');
    $backupdir = ZH_PATH . DS . 'backup' . DS . $name;

    if (!is_dir($backupdir)) {
        mkdir($backupdir, 0777, true);
    }
    
    $updatedata = array("name"=>$name,"addtime"=>date("Y-m-d H:i:s"));
    D("zhmvc_mysql")->LinkInsert($updatedata);
    
    $c = new \ZHCONFIG\ZhConfig();
    
    $backup = \lotofbadcode\phpextend\databackup\BackupFactory::instance('mysql', '' . $c->getHost() . ':3306', '' . $c->getDatabase() . '', '' . $c->getUser() . '', '' . $c->getPassWord() . '');
    $backup->setbackdir($backupdir) //设置备份目录
    ->setvolsize(0.2); //设置分卷大小
    do {
        $result = $backup->backup();
        echo str_repeat(' ', 1000); //这里会把浏览器缓存装满
        ob_flush();
        flush();
        if ($result['totalpercentage'] > 0) {
            echo '完成' . $result['totalpercentage'] . '%<br />';
        }
    } while ($result['totalpercentage'] < 100);
    
}

function huifu()
{
	$postid=SafeRequest(getPGC('id'),0);

	$bind=array("id"=>$postid);
	$data = D("zhmvc_mysql")->where($bind)->getLinkOne("id,`name`,`addtime`");
	$name=$data['name'];
	$backupdir = ZH_PATH . DS . 'backup' . DS . $name;
	
	$c = new \ZHCONFIG\ZhConfig();
	
	$recovery = \lotofbadcode\phpextend\databackup\RecoveryFactory::instance('mysql', '' . $c->getHost() . ':3306', '' . $c->getDatabase() . '', '' . $c->getUser() . '', '' . $c->getPassWord() . '');
	$recovery->setSqlfiledir($backupdir);
	do
	{
	    $result = $recovery->recovery();
	    echo str_repeat(' ', 1000);
	    ob_flush();
	    flush();
	    if ($result['totalpercentage'] > 0)
	    {
	        echo '完成' . $result['totalpercentage'] . '%<br />';
	    }
	} while ($result['totalpercentage'] < 100);
}

function del($db_pre)
{
    $postid = SafeRequest(getPGC('id'), 0);
    $bind=array("id"=>$postid);
    $data = D("zhmvc_mysql")->where($bind)->getLinkOne("id,`name`,`addtime`");
    $name=$data['name'];
    $backupdir = ZH_PATH . DS . 'backup' . DS . $name;
    
    deleteDir($backupdir);
    
    $bind=array("id"=>$postid);
    D("zhmvc_mysql")->LinkDelete($bind);
    echo "删除成功";
    
}

function deleteDir($dir)
{
    if (!$handle = @opendir($dir)) {
        return false;
    }
    while (false !== ($file = readdir($handle))) {
        if ($file !== "." && $file !== "..") {       //排除当前目录与父级目录
            $file = $dir . '/' . $file;
            if (is_dir($file)) {
                deleteDir($file);
            } else {
                @unlink($file);
            }
        }
    }
    @rmdir($dir);
}

?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
	<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
